import { computed } from 'vue'
import store from '@/store/index'

export default (Vue) => {
  /**自定义按钮权限指令 */
  Vue.directive('has', {
    mounted(el, binding) {
      //获取按钮权限
      if (!binding.value) {
        return
      }
      if (!Vue.config.globalProperties.$_has(binding.value)) {
        //移除不匹配的按钮
        el.parentNode.removeChild(el)
      }
    },
  })

  //检查权限方法
  Vue.config.globalProperties.$_has = function (value) {
    let isExist = false
    const HSuserInfo = sessionStorage.getItem('HS-userInfoNew')
    const curUserInfo = JSON.parse(HSuserInfo)
    const list = computed(() => curUserInfo.content.permissionCodes)
    let btnPermsArr = list.value //store.state.user.permissionCodeList //['user:add', 'user:edit', 'user:del'] //获取从服务器请求存储本地的按钮权限
    if (btnPermsArr.includes(value)) {
      isExist = true
    }
    return isExist
  }
}
